package com.coder.algorithm.leetcode

/**
 * [73. 矩阵置零](https://leetcode-cn.com/problems/set-matrix-zeroes/)
 * [面试题 01.08. 零矩阵](https://leetcode-cn.com/problems/zero-matrix-lcci/)
 *
 * @author yuhus
 * @date: 2022/3/14
 */
class l_0073 {
    /**
     * 标记-清除
     */
    fun setZeroes(matrix: Array<IntArray>): Unit {
        val row = matrix.size
        val col = matrix[0].size
        val rows = BooleanArray(row)
        val cols = BooleanArray(col)
        // 标记
        for (i in 0 until row) {
            for (j in 0 until col) {
                if (matrix[i][j] == 0) {
                    rows[i] = true
                    cols[j] = true
                }
            }
        }
        // 清除
        for (i in 0 until row) {
            for (j in 0 until col) {
                if (rows[i] || cols[j]) {
                    matrix[i][j] = 0
                }
            }
        }
    }
}